package com.nuanshui.heatedloan.pageable;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.google.common.collect.Maps;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class Pager<T> extends Pagination implements Pageable, Serializable {


    private static final long serialVersionUID = 5706036388123397753L;

    private int page; //第几页

    private Sort sort;

    public List<T> data;

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Pager() {
        super();
    }

    public Pager(Pageable pager) {
        super(pager.getPageNumber(), pager.getPageSize());
        this.page = pager.getPageNumber();
    }

    public Pager(int page, int size) {
        super(page, size);
        this.page = page;
    }

    public Pager(int page, int size,Sort sort) {
        this(page, size);
        this.sort=sort;
    }

    public int getPage() {
        return page;
    }


    public Map<String, Object> asHashMap()
    {
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", this.getData());
        map.put("total", this.getTotal());
        return map;
    }

    protected static int offsetCurrent(int current, int size) {
        if (current > 0) {
            return current * size;
        }
        return 0;
    }


    @Override
    public int getPageSize() {
        return this.getSize();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#getPageNumber()
     */
    @Override
    public int getPageNumber() {
        return page;
    }

    @Override
    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort=sort;
    }

    @Override
    public Pageable next() {
        return null;
    }

    @Override
    public Pageable previousOrFirst() {
        return null;
    }

    @Override
    public Pageable first() {
        return null;
    }

    @Override
    public String toString() {
        return this.getOffset() + ":" + this.getSize();
    }
}

